// pages/groupbuying/groupbuyingList/groupbuyingList.js
var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dayuba: false,
    dayushier: false,
    dayushiliu: false,
    dayuershi: false,
    weikaishi: false,
    changciname: '早上',
    seckillList: [],
    page: 1,
    limit: 10
  },
  seckillDetail: function (e) {
    let that = this;
    if (that.data.weikaishi) {
      wx.showToast({
        title: '抢购暂未开始！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (that.data.dayuba && that.data.changciname == '早上') {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../seckillDetail/seckillDetail?id=' + id
      })
    }
    else if (that.data.dayushier && that.data.changciname == '中午') {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../seckillDetail/seckillDetail?id=' + id
      })
    }
    else if (that.data.dayushiliu && that.data.changciname == '下午') {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../seckillDetail/seckillDetail?id=' + id
      })
    }
    else if (that.data.dayuershi && that.data.changciname == '晚上') {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../seckillDetail/seckillDetail?id=' + id
      })
    }
    else
    {
      wx.showToast({
        title: '抢购暂未开始！',
        icon: 'none',
        duration: 3000
      })
      return false;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.qianggoutime();
  },
  //点击切换抢购时间段
  shijianduan: function (e) {
    let that = this;
    var changciname = e.currentTarget.dataset.changciname;
    that.setData({
      changciname: changciname
    })
    that.getSeckillList();
  },

  qianggoutime: function () {
    let that = this;
    var nowtime = that.format("yyyy-MM-dd hh:mm:ss");
    // var nowtime1="2021-07-06 00:00:01";
    var nowhour = nowtime.split(" ")[1];
    let badian = "08:00:00";
    let shierdian = "12:00:00";
    let shiliudian = "16:00:00";
    let ershidian = "20:00:00";
    var dayuba = that.compareTime(badian, nowhour);
    if (dayuba) {
      that.setData({
        dayuba: true,
        dayushier: false,
        dayushiliu: false,
        dayuershi: false,
        changciname: '早上'
      })
    } else {
      that.setData({
        weikaishi: true,
        changciname: '未开始'
      })
    }
    var dayushier = that.compareTime(shierdian, nowhour);
    if (dayushier) {
      that.setData({
        dayuba: false,
        dayushier: true,
        dayushiliu: false,
        dayuershi: false,
        changciname: '中午'
      })
    }
    var dayushiliu = that.compareTime(shiliudian, nowhour);
    if (dayushiliu) {
      that.setData({
        dayuba: false,
        dayushier: false,
        dayushiliu: true,
        dayuershi: false,
        changciname: '下午'
      })
    }
    var dayuershi = that.compareTime(ershidian, nowhour);
    if (dayuershi) {
      that.setData({
        dayuba: false,
        dayushier: false,
        dayushiliu: false,
        dayuershi: true,
        changciname: '晚上'
      })
    }
    that.getSeckillList();
  },
  // 如果time2大于time1 返回true 否则 返回false
  compareTime: function (jiudianhour, nowhour) {
    let that = this;
    if (that.time_to_sec(nowhour) - that.time_to_sec(jiudianhour) > 0) {
      return true;
    }
    return false;
  },
  // 对Date的扩展，将 Date 转化为指定格式的String
  // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
  // 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  // 例子：
  // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

  format: function (fmt) { // author: meizz
    var tday = new Date();
    var o = {
      "M+": tday.getMonth() + 1, // 月份
      "d+": tday.getDate(), // 日
      "h+": tday.getHours(), // 小时
      "m+": tday.getMinutes(), // 分
      "s+": tday.getSeconds(), // 秒
      "q+": Math.floor((tday.getMonth() + 3) / 3), // 季度
      "S": tday.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (tday.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },

  //将时分秒转为时间戳
  time_to_sec: function (time) {
    if (time !== null) {
      var s = "";
      var hour = time.split(":")[0];
      var min = time.split(":")[1];
      var sec = time.split(":")[2];
      s = Number(hour * 3600) + Number(min * 60) + Number(sec);
      return s;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getSeckillList: function () {
    let that = this;
    util.request(api.SeckillList, {
      changciname: that.data.changciname,
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        that.setData({
          seckillList: res.data
        });
      }
    });
  }
})